package servicesImp.upms;

import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.upms.PermissionRoleServices;
import services.upms.RoleServices;
import util.key.IDFactory;
import wt.dao.upms.RoleMapper;
import wt.entity.upms.PermissionRole;
import wt.entity.upms.PermissionRoleExample;
import wt.entity.upms.Role;
import wt.entity.upms.RoleExample;
import wt.staticData.TableName;

import java.util.List;


/**
 * Created by Administrator on 2017/9/7.
 */
@Service
public class RoleServicesImp extends IBaseServicesWithoutBLOBsImpl<Role, RoleExample> implements RoleServices {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionRoleServices permissionRoleServices;

    @Override
    public IBaseMapperWithoutBLOBs<Role, RoleExample> getMapper() {
        return roleMapper;
    }
    @Override
    public String getTableName() {
        return  TableName.UPMS_ROLE;
    }

    @Transactional
    public void insertSelective(Role role, List<String> permissionIdList) throws Exception {
        getCurrentProxy().insertSelective(role);
        
        PermissionRole permissionRole = new PermissionRole();
        permissionRole.setRoleId(role.getId());
        for (int i = 0; i < permissionIdList.size(); i++) {
            permissionRole.setId(IDFactory.getSnowflakeId());
            permissionRole.setPermissionId(permissionIdList.get(i));
            permissionRoleServices.insertSelective(permissionRole);
        }
    }

    @Transactional
    public void updateByPrimaryKeySelective(Role role, List<String> permissionIdList) throws Exception {


        getCurrentProxy().updateByPrimaryKeySelective(role);


        PermissionRoleExample permissionRoleExample = new PermissionRoleExample();
        permissionRoleExample.createCriteria().andRoleIdEqualTo(role.getId());
        List<PermissionRole>  permissionRoleList=permissionRoleServices.selectByExample(permissionRoleExample);

        for (int i = 0;i  < permissionRoleList.size(); i++) {//去除相同部分
            PermissionRole permissionRole=permissionRoleList.get(i);
            for (int j = 0;j  <permissionIdList.size() ; j++) {
                if (permissionRole.getPermissionId().equals(permissionIdList.get(j))){
                    permissionIdList.remove(j);
                    permissionRoleList.remove(i);
                    i--;
                    break;
                }
            }
        }


        for(PermissionRole permissionRole:permissionRoleList){//删除
            permissionRoleServices.deleteByPrimaryKey(permissionRole.getId());
        }

        PermissionRole permissionRole = new PermissionRole();
        permissionRole.setRoleId(role.getId());

        for (String id : permissionIdList) {//添加
            permissionRole.setId(IDFactory.getSnowflakeId());
            permissionRole.setPermissionId(id);
            permissionRoleServices.insertSelective(permissionRole);
        }


    }


}
